<?php

namespace app\common\service;

use app\helpers\Common;
use app\common\model\Config as ConfigMdl;

use think\facade\Cache;
use think\facade\Db;

class Config
{

    //public $expires = 86400;

    /**
     * 删除配置缓存
     */
    public static function delCache($configName)
    {
        if ($configName) {
            Cache::delete('cache_' . $configName);
        } else {
            Cache::delete('cache_config');
        }
    }

    /**
     * 获取系统配置(通用)
     *
     * @param string $key 配置键值,都小写
     * @return mixed
     */
    public static function getConfig($key, $expires = 86400)
    {
        if (!is_string($key) || empty($key)) {
            return false;
        }

        $option = cache('cache_' . $key);
        if (empty($option)) {
            //$config = Db::name('configs')->where(['name' => $key])->field('id, name, value')->find();
            $config = Db::name('options')->where(['name' => $key])->field('id, name, value')->find();
            if ($config) {
                $option = $config['value'];
                if ($option) {
                    $option = json_decode($option, true);
                    // 缓存
                    cache('cache_' . $key, $option, $expires);
                }
            }
        }

        return $option;
    }

    /**
     * 获取文件上传配置
     */
    public static function getUploadSetting()
    {
        $upload_setting = self::getConfig('upload_setting');
        if (empty($upload_setting)) {
            $upload_setting = [
                'storage' => 'local',
                'mimetype' => [
                    'image' => [
                        'max_filesize' => '10240', // 单位KB
                        'extensions' => 'jpg,jpeg,png,gif,bmp4'
                    ],
                    'video' => [
                        'max_filesize' => '10240',
                        'extensions' => 'mp4,avi,wmv,rm,rmvb,mkv'
                    ],
                    'audio' => [
                        'max_filesize' => '10240',
                        'extensions' => 'mp3,wma,wav'
                    ],
                    'file' => [
                        'max_filesize' => '10240',
                        'extensions' => 'txt,pdf,doc,docx,xls,xlsx,ppt,pptx,zip,rar'
                    ]
                ]
            ];
        }

        if (empty($upload_setting['upload_max_filesize'])) {
            $upload_max_filesize_setting = [];
            foreach ($upload_setting['mimetype'] as $setting) {
                $extensions = explode(',', trim($setting['extensions']));
                if (!empty($extensions)) {
                    $upload_max_filesize = intval($setting['max_filesize']) * 1024; // 转化成KB
                    foreach ($extensions as $ext) {
                        if (!isset($upload_max_filesize_setting[$ext]) || $upload_max_filesize > $upload_max_filesize_setting[$ext] * 1024) {
                            $upload_max_filesize_setting[$ext] = $upload_max_filesize;
                        }
                    }
                }
            }

            $upload_setting['upload_max_filesize'] = $upload_max_filesize_setting;
            cache('cache-upload_setting', $upload_setting, 3600 * 6);
        } else {
            $upload_setting = cache('cache-upload_setting');
        }

        return $upload_setting;
    }
}